package com.ruoyi.order.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.order.domain.WxVhiceodr;
import com.ruoyi.order.domain.dto.AssignOrders;
import com.ruoyi.order.domain.dto.WxVhiceodrDto;
import com.ruoyi.order.domain.ex.OrderException;
import com.ruoyi.order.domain.vo.WxVhiceodrVo;
import com.ruoyi.order.service.IWxVhiceodrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


/**
 * 租车订单实例Controller
 * 
 * @author ruoyi
 * @date 2024-08-29
 */
@RestController
@RequestMapping("/merchant/vhiceodr")
public class WxVhiceodrController extends BaseController {
    @Autowired
    private IWxVhiceodrService wxVhiceodrService;
    private static final String FILE_DELIMETER = ",";

    /**
     * 查询租车订单实例列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:vhiceodr:list')")
    @GetMapping("/list")
    public TableDataInfo list(WxVhiceodrDto wxVhiceodr) {
        startPage();
        List<WxVhiceodrVo> list = wxVhiceodrService.selectWxVhiceodrList(wxVhiceodr);
        return getDataTable(list);
    }

    @GetMapping("/staticsOrderQuantity/{deptId}")
    public AjaxResult get(@PathVariable Long deptId) {
        return success(wxVhiceodrService.selectSumQuantity(deptId));
    }

    /**
     * 导出租车订单实例列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:vhiceodr:export')")
    @Log(title = "租车订单实例", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxVhiceodrDto wxVhiceodr) {
        List<WxVhiceodrVo> list = wxVhiceodrService.selectWxVhiceodrList(wxVhiceodr);
        ExcelUtil<WxVhiceodrVo> util = new ExcelUtil<WxVhiceodrVo>(WxVhiceodrVo.class);
        util.exportExcel(response, list, "租车订单实例数据");
    }

    /**
     * 获取租车订单实例详细信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:vhiceodr:edit')")
    @GetMapping(value = "/{orderNum}")
    public AjaxResult getInfo(@PathVariable("orderNum") String orderNum) {
        return success(wxVhiceodrService.selectWxVhiceodrByNum(orderNum));
    }

    /**
     * 新增租车订单实例
     */
    @PreAuthorize("@ss.hasPermi('merchant:vhiceodr:add')")
    @Log(title = "租车订单实例", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxVhiceodr wxVhiceodr) {
        return AjaxResult.success(wxVhiceodrService.generateBaseOrder(wxVhiceodr));
    }

    /**
     * 修改租车订单实例
     */
    @PreAuthorize("@ss.hasPermi('merchant:vhiceodr:edit')")
    @Log(title = "租车订单实例", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxVhiceodr wxVhiceodr) {
        return toAjax(wxVhiceodrService.updateWxVhiceodr(wxVhiceodr));
    }

    /**
     * 修改租车订单金额
     */
    @PreAuthorize("@ss.hasPermi('merchant:vhiceodr:edit')")
    @Log(title = "租车订单实例", businessType = BusinessType.UPDATE)
    @PutMapping("/editMoney")
    public AjaxResult editMoney(@RequestBody WxVhiceodr wxVhiceodr) {
        return toAjax(wxVhiceodrService.editMoney(wxVhiceodr));
    }

    /**
     * 删除租车订单实例
     */
    @PreAuthorize("@ss.hasPermi('merchant:vhiceodr:remove')")
    @Log(title = "租车订单实例", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(wxVhiceodrService.deleteWxVhiceodrByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('merchant:vhiceodr:list')")
    @GetMapping("/getOeaiRcrList/{voNum}")
    public AjaxResult getVhiceordrList(@PathVariable String voNum) {
        return AjaxResult.success(wxVhiceodrService.selectWxVhiceodrRecordList(voNum));
    }

    @Log(title = "放弃订单", businessType = BusinessType.UPDATE)
    @PostMapping("/abandonOrder")
    public AjaxResult abandonOrder(@RequestBody WxVhiceodr wxVhiceodr) {
        return AjaxResult.success(wxVhiceodrService.abandonOrder(wxVhiceodr));
    }

    @Log(title = "商户弃单", businessType = BusinessType.UPDATE)
    @PostMapping("/refuseOrder")
    public AjaxResult refuseOrder(@RequestBody WxVhiceodrDto wxVhiceodr) {
        return AjaxResult.success(wxVhiceodrService.refuseOrder(wxVhiceodr));
    }

    /**
     * 订单指派
     *
     * @param assignOrders 指派对象
     * @return 指派结果
     */
    @PostMapping("/assignOrders")
    @PreAuthorize("@ss.hasPermi('merchant:vhiceodr:edit')")
    @Log(title = "指派订单", businessType = BusinessType.UPDATE)
    public AjaxResult assignOrders(@RequestBody AssignOrders assignOrders) {
        return toAjax(wxVhiceodrService.assignOrders(assignOrders));
    }

    /**
     *  修改订单驾驶员信息
     * @param wxVhiceodr
     * @return 结果
     */
    @PostMapping("/changeOrderDriver")
    @Log(title = "修改订单驾驶员信息", businessType = BusinessType.UPDATE)
    public AjaxResult changeOrderDriver(@RequestBody WxVhiceodr wxVhiceodr) {
        try {
            WxVhiceodr wxVhiceodr1  =new WxVhiceodr();
            wxVhiceodr1.setId(wxVhiceodr.getId());
            wxVhiceodr1.setDriverName(wxVhiceodr.getDriverName());
            wxVhiceodr1.setDriverPhone(wxVhiceodr.getDriverPhone());
            wxVhiceodr1.setDriverIdCard(wxVhiceodr.getDriverIdCard());
            return success(wxVhiceodrService.updateWxVhiceodr(wxVhiceodr1));
        } catch (OrderException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/acceptOrders")
    @Log(title = "接取订单", businessType = BusinessType.UPDATE)
    public AjaxResult acceptOrders(@RequestBody WxVhiceodrDto wxVhiceodr) {
        try {
            return AjaxResult.success(wxVhiceodrService.acceptOrders(wxVhiceodr));
        } catch (OrderException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 上传车辆视频接口
     */
    @PostMapping("/uploadVideo")
    public AjaxResult uploadVideo(MultipartFile file) throws Exception {
        if (!file.isEmpty()) {
            String videourl = FileUploadUtils.upload(RuoYiConfig.getVehVideoPath(), file, MimeTypeUtils.VIDEO_EXTENSION);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", "/prod-api" + videourl);
            return ajax;
        }

        return error("上传车辆视频异常，请联系管理员");
    }

    /**
     * 订单身份证,驾驶证图片上传
     */
    @Log(title = "订单相关图片", businessType = BusinessType.UPDATE)
    @PostMapping("/upload/files")
    @Anonymous
    public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception {
        List<String> urls = new ArrayList<String>();
        if (!files.isEmpty()) {
            for (MultipartFile file : files) {
                String orderinfourl = FileUploadUtils.upload(RuoYiConfig.getOrderCertiPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
                urls.add("/prod-api" + orderinfourl);
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }
    /**
     * 订单车损图片上传
     */
    @Log(title = "订单车损相关图片", businessType = BusinessType.UPDATE)
    @PostMapping("/upload/carfiles")
    @Anonymous
    public AjaxResult carFiles(List<MultipartFile> files) throws Exception {
        List<String> urls = new ArrayList<String>();
        if (!files.isEmpty()) {
            for (MultipartFile file : files) {
                String vehdamageurl = FileUploadUtils.upload(RuoYiConfig.getVehDamagePath(), file, MimeTypeUtils.IMAGE_EXTENSION);
                urls.add("/prod-api" + vehdamageurl);
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }



    @PostMapping("/saveUpdate")
    @Log(title = "订单用户取车接口", businessType = BusinessType.UPDATE)
    public AjaxResult saveUpdate(@RequestBody WxVhiceodrDto wxVhiceodr) {
        try {
            return AjaxResult.success(wxVhiceodrService.saveUpdate(wxVhiceodr));
        } catch (OrderException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/pickUpCar")
    @Log(title = "订单用户取车接口", businessType = BusinessType.UPDATE)
    public AjaxResult pickUpCar(@RequestBody WxVhiceodrDto wxVhiceodr) {
        try {
            return AjaxResult.success(wxVhiceodrService.pickUpCar(wxVhiceodr));
        } catch (OrderException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/returnCar")
    @Log(title = "订单用户取车接口", businessType = BusinessType.UPDATE)
    public AjaxResult returnCar(@RequestBody WxVhiceodrDto wxVhiceodr) {
        try {
            return AjaxResult.success(wxVhiceodrService.returnCar(wxVhiceodr));
        } catch (OrderException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Log(title = "订单维修发票图片", businessType = BusinessType.UPDATE)
    @PostMapping("/upload/invoicefiles")
    @Anonymous
    public AjaxResult invoiceFiles(List<MultipartFile> files) throws Exception {
        List<String> urls = new ArrayList<String>();
        if (!files.isEmpty()) {
            for (MultipartFile file : files) {
                String vehinvoiceurl = FileUploadUtils.upload(RuoYiConfig.getVehInvoicePath(), file, MimeTypeUtils.IMAGE_EXTENSION);
                urls.add("/prod-api" + vehinvoiceurl);
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }

    @Log(title = "结算订单", businessType = BusinessType.UPDATE)
    @PostMapping("/orderSettlement")
    @Anonymous
    public AjaxResult orderSettlement(@RequestBody WxVhiceodr wxVhiceodr){
        return success(wxVhiceodrService.orderSettlement(wxVhiceodr));
    }

    @Log(title = "退还租车押金", businessType = BusinessType.UPDATE)
    @PostMapping("/rentalDepositRefund")
    @Anonymous
    public AjaxResult rentalDepositRefund(@RequestBody WxVhiceodrDto wxVhiceodr){
        return AjaxResult.success(wxVhiceodrService.rentalDepositRefund(wxVhiceodr));
    }

    @Log(title = "退还违章押金", businessType = BusinessType.UPDATE)
    @PostMapping("/violationDepositRefund")
    @Anonymous
    public AjaxResult violationDepositRefund(@RequestBody WxVhiceodrDto wxVhiceodr){
        return AjaxResult.success(wxVhiceodrService.violationDepositRefund(wxVhiceodr));
    }

    @Log(title = "退单", businessType = BusinessType.UPDATE)
    @PostMapping("/refundOrder")
    @Anonymous
    public AjaxResult refundOrder(@RequestBody WxVhiceodr wxVhiceodr){
        return AjaxResult.success(wxVhiceodrService.refundOrder(wxVhiceodr));
    }
}
